<?php

namespace paymch\controllers;

use Yii;
use yii\web\Response;
use paymch\components\BaseController as Controller;
use common\models\trade\Trade;
use paymch\models\UnifiedOrder;
use paymch\models\TradeClose;
use paymch\models\TradeRefund;

/**
 * TradeController 用于交易处理
 * 
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class TradeController extends Controller {

    /**
     * 设置当前控制器下响应格式为JSON
     */
    public function behaviors() {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return parent::behaviors();
    }

    /**
     * 创建交易在线下单（统一下单）
     */
    public function actionCreate() {

        //统一下单参数内容
        $post = Yii::$app->request->post();

        //记录日志-保存下单参数
        parent::log('UnifiedOrder', [
            'postData' => $post
        ]);

        //定义默认返回内容
        $status = false;
        $msg = '';

        //统一下单处理
        $model = new UnifiedOrder();
        if ($model->load($post, '')) {
            $trade_id = $model->save($msg);
            if ($trade_id !== false) {
                //下单成功时执行交易回调
                $status = true;
                $this->setTradeId($trade_id);
            }
        } else {
            $msg = 'UnifiedOrder request data load faild.';
        }

        return compact('status', 'msg', 'trade_id');
    }

    /**
     * 商户业务订单取消交易
     * @return json
     */
    public function actionClose() {

        //交易关闭处理参数内容
        $post = Yii::$app->request->post();

        //记录日志-保存下单参数
        parent::log('TradeClose', [
            'postData' => $post
        ]);

        //定义默认返回内容
        $status = false;
        $msg = 'Success';

        //交易关闭处理
        $model = new TradeClose();
        if ($model->load($post, '')) {
            $status = $model->save($msg);
        } else {
            $msg = 'TradeClose request data load faild.';
        }

        return compact('status', 'msg');
    }

    /**
     * 交易重置
     */
    public function actionReset($id) {
        $status = false;
        $trade = Trade::findOne($id);
        if ($trade) {
            $trade->status = Trade::STATUS_WAITPAY;
            $status = $trade->save();
            if ($status) {
                $this->setTradeId($trade->id);
            }
        }
        if ($status == true) {
            return $this->redirect(Yii::$app->request->referrer);
        }
        return compact('status');
    }

    /**
     * 交易退款
     * @return json
     */
    public function actionRefund() {

        //交易退款处理参数内容
        $post = Yii::$app->request->post();

        //记录日志-保存退款参数
        parent::log('TradeRefund', [
            'postData' => $post
        ]);

        $status = false;
        $msg = 'Success';

        $model = new TradeRefund();
        if ($model->load($post, '')) {
            $status = $model->save($msg);
        } else {
            $msg = '请求数据失败！';
        }
        return compact('status', 'msg');
    }

    /**
     * 重新通知
     */
    public function actionNotify($id) {
        $status = false;
        $trade = Trade::findOne($id);
        //通知业务订单成功了
        if ($trade->status == Trade::STATUS_SUCCESS) {
            $notify = new \paymch\models\BusinessNotify($trade->id);
            $status = $notify->save($msg);
        } else {
            $msg = '交易状态未成功！';
        }
        return compact('status', 'msg');
    }

}
